<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Wyp
 */

namespace App\Modules\Live\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class LiveListCriteria extends BaseCriterias
{
    private $lon;
    private $lat;
    private $distance;
    public function __construct($lon,$lat,$distance)
    {
        $this->lon = $lon;
        $this->lat = $lat;
        $this->distance = $distance;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $select = [
            'hx_live.id',
            'hx_user.id as user_id',
            'hx_user.nick as nick',
            'hx_user.portrait as portrait',
            'hx_live.member_num',
            'hx_live.picture',
            'hx_live.rtmp',
            'hx_live.flv',
            'hx_store.name as store',
            'hx_store.id as store_id',
            "ROUND(
                6378.138 * 2 * ASIN(
                    SQRT(
                        POW(
                            SIN(
                                (
                                    ".$this->lat."  * PI() / 180 - hx_store.latitude * PI() / 180
                                ) / 2
                            ),
                            2
                        ) + COS(".$this->lat." * PI() / 180) * COS(hx_store.latitude * PI() / 180) * POW(
                            SIN(
                                (
                                    ".$this->lon." * PI() / 180 - hx_store.longitude * PI() / 180
                                ) / 2
                            ),
                            2
                        )
                    )
                ) * 1000
            ) /1000 AS distance"
        ];

        $select = implode($select,',');
        $where = ' hx_live.status = 1 ';
        if($this->distance) {
            $where.= " and ROUND(
            6378.138 * 2 * ASIN(
                SQRT(
                    POW(
                        SIN(
                            (
                                ".$this->lat."  * PI() / 180 - hx_store.latitude * PI() / 180
                            ) / 2
                        ),
                        2
                    ) + COS(".$this->lat." * PI() / 180) * COS(hx_store.latitude * PI() / 180) * POW(
                        SIN(
                            (
                                ".$this->lon." * PI() / 180 - hx_store.longitude * PI() / 180
                            ) / 2
                        ),
                        2
                    )
                )
            ) * 1000
        ) < " . $this->distance * 1000;
        }
        return $model->selectRaw($select)
            ->leftJoin('user','live.user_id','user.id')
            ->leftJoin('picture','user.portrait','picture.id')
            ->leftJoin('store','live.store_id','store.id')
            ->whereRaw($where)
            ->orderBy('live.is_hot','desc')
            ->orderBy('live.hot_time_at','desc')
            ->orderBy('distance','asc')
            ->orderBy('live.created_at','desc')
            ;
    }
}
